﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SimpleActor
{
    internal class ExpressionHelper
    {
        public static string GetMemberName<TDelegate>(Expression<TDelegate> lambda)
        {
            if (lambda == null)
            {
                throw new ArgumentNullException("lambda");
            }

            string ret;
            var body = lambda.Body;

            if (body is MethodCallExpression)
            {
                ret = ((MethodCallExpression)body).Method.Name;
            }
            else if (body is MemberExpression)
            {
                ret = ((MemberExpression)body).Member.Name;
            }
            else
            {
                throw new ArgumentException("lambda");
            }
            return ret;
        }

        public static bool ValueIncluded<TDelegate>(Expression<TDelegate> lambda)
        {
            if (lambda == null)
            {
                throw new ArgumentNullException("lambda");
            }

            bool ret;
            var body = lambda.Body;

            if (body is MethodCallExpression)
            {
                ret = ((MethodCallExpression)body).Arguments.Any(x => x.NodeType != ExpressionType.Parameter);
            }
            else if (body is MemberExpression)
            {
                ret = false;
            }
            else
            {
                throw new ArgumentException("lambda");
            }

            return ret;
        }

    }
}
